package titancommon.node.tasks;


import java.awt.Color;
import java.awt.Panel;
import java.awt.Image;
import java.awt.Graphics;
import titancommon.gui.TitanFrame;
import titancommon.node.DataPacket;
import titancommon.node.TitanTask;
import titancommon.tasks.DemoGUI;


/**
 * Description: @see titancommon.tasks.DemoGUI
 *
 * please note that all activities and images are hard-coded. this was just a demonstration for our activity recognition
 *
 * @author Benedikt Koeppel <bkoeppel@ee.ehtz.ch>
 * @author Jonas Huber <huberjo@ee.ethz.ch>
 */
public class EDemoGUI extends DemoGUI implements ExecutableTitanTask {

    private TitanTask tTask;
    private DemoFrame dFrame;
    private Image [] pictures;
    private Image curPic;
    
    public void setTitanTask(TitanTask tsk) {
        tTask = tsk;
    }

    public boolean setExecParameters(short[] param) {
        return true;
    }

    public void init() {
        dFrame = new DemoFrame();
        dFrame.setVisible(true);
    }

    public void inDataHandler(int port, DataPacket data) {

        if ( port != 0 ) {
            // TODO: What to do here? This question holds also for other servcies!
            System.err.println("[EDemoGUI] Received data on invalid port!");
            return;
        }

        // Todo: maybe look for label ids in the label config file instead
        // of hard coding them here...
        switch ( data.sdata[0] ) {
            case 1:
                dFrame.m_panel.activity.setText("seated");
                curPic = pictures[0];
                break;
            case 2:
                dFrame.m_panel.activity.setText("standing");
                curPic = pictures[1];
                break;
            case 3:
                dFrame.m_panel.activity.setText("walking");
                curPic = pictures[2];
                break;
            case 4:
                dFrame.m_panel.activity.setText("running");
                break;
            default:
                dFrame.m_panel.activity.setText("unknown activity");
                curPic = null;
        }
        dFrame.m_panel.picture.repaint();
    }

    /**
     * This is the GUI.
     */
    private class DemoFrame extends TitanFrame {

        /*
         * EBTStatusGUI needs a possibility to communicate with the embedded
         * panel.
         */
        private DemoPanel m_panel;

        public DemoFrame() {
            super();
            // Registering this as a new frame with the TitanFrame class
            TitanFrame.registerFrame(this);

            // Set the conent of the main panel of this TitanFrame to our
            // acutual GUI which is implemented as a private Panel below.
            m_panel = new DemoPanel();
            setTitanFramePanel( m_panel );
        }

        /**
         * This class holds the actual GUI to be displayed within the TitanFrame.
         */
        private class DemoPanel extends Panel {
            
            /** Creates new panel. */
            public DemoPanel() {
                
                // Load images
                pictures = new Image[3];
                pictures[0] = getToolkit().getImage("pics/sit300.gif");
                pictures[1] = getToolkit().getImage("pics/stand300.gif");
                pictures[2] = getToolkit().getImage("pics/walk300.gif");

                initComponents();
            }

            /** This method is called from within the constructor to
             * initialize the form.
             * WARNING: Do NOT modify this code. The content of this method is
             * always regenerated by the Form Editor.
             */
            // <editor-fold defaultstate="collapsed" desc="Generated Code">
            private void initComponents() {

                setLayout(new java.awt.BorderLayout());
                
                // Create canvas for image display
                picture = new java.awt.Canvas() {
                    public void paint( Graphics g)
                    {
                        g.drawImage( curPic, 0, 0, this );
                    }
                };
                picture.setSize(300,290);
                add(picture, java.awt.BorderLayout.CENTER);

                // Add the label which shall display the activity.
                activity = new java.awt.Label();
                activity.setText("Nothing detected!");
                add(activity, java.awt.BorderLayout.SOUTH);
                
            }// </editor-fold>

            /** Exit the Application */
            private void exitForm(java.awt.event.WindowEvent evt) {
                //System.exit(0); // TODO: BAD!!!
            }
            
            // Variables declaration - do not modify
            private java.awt.Label activity;
            private java.awt.Canvas picture;
            // End of variables declaration
        };
    };
}